using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Data;
 using System.Data.SqlClient;
 using MinhTam.DataAccess.Web;
//───────────────────────────────────────────────────────────────────────────────────────
 namespace MinhTam.Objects
  { 
 public class KHACH_SAN:  SQLConnectWeb { 
 public static string sTableName= "KHACH_SAN"; 
   public string KHACH_SAN_ID;
   public string TEN_KHACH_SAN;
   public string DIA_CHI;
   public string SO_DIEN_THOAI;
   public string TINH_THANH;
   #region DataColumn Name ;
 public static  string cl_KHACH_SAN_ID="KHACH_SAN_ID" ;
 public static  string cl_TEN_KHACH_SAN="TEN_KHACH_SAN" ;
 public static  string cl_DIA_CHI="DIA_CHI" ;
 public static  string cl_SO_DIEN_THOAI="SO_DIEN_THOAI" ;
 public static  string cl_TINH_THANH="TINH_THANH" ;
 #endregion;
//───────────────────────────────────────────────────────────────────────────────────────
       public KHACH_SAN() {}
//───────────────────────────────────────────────────────────────────────────────────────
       public KHACH_SAN(
         string sKHACH_SAN_ID,
         string sTEN_KHACH_SAN,
         string sDIA_CHI,
         string sSO_DIEN_THOAI,
         string sTINH_THANH){
         this.KHACH_SAN_ID= sKHACH_SAN_ID;
         this.TEN_KHACH_SAN= sTEN_KHACH_SAN;
         this.DIA_CHI= sDIA_CHI;
         this.SO_DIEN_THOAI= sSO_DIEN_THOAI;
         this.TINH_THANH= sTINH_THANH;
}
//───────────────────────────────────────────────────────────────────────────────────────
       public static KHACH_SAN Create_KHACH_SAN ( string sKHACH_SAN_ID  ){
    DataTable dt=SearchByKHACH_SAN_ID(sKHACH_SAN_ID) ;
    if(dt!=null && dt.Rows.Count>0) 
      return new KHACH_SAN(dt,0);
      return null;
}
//───────────────────────────────────────────────────────────────────────────────────────
   private static string s_Select()
    {
   return " SELECT T.* FROM KHACH_SAN AS T";
    }
//───────────────────────────────────────────────────────────────────────────────────────
 public KHACH_SAN( DataTable table,int pos)
{
         this.KHACH_SAN_ID= table.Rows[pos]["KHACH_SAN_ID"].ToString();
         this.TEN_KHACH_SAN= table.Rows[pos]["TEN_KHACH_SAN"].ToString();
         this.DIA_CHI= table.Rows[pos]["DIA_CHI"].ToString();
         this.SO_DIEN_THOAI= table.Rows[pos]["SO_DIEN_THOAI"].ToString();
         this.TINH_THANH= table.Rows[pos]["TINH_THANH"].ToString();
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByKHACH_SAN_ID(string sKHACH_SAN_ID)
{
          string sqlSelect= s_Select()+ " WHERE KHACH_SAN_ID  ="+ sKHACH_SAN_ID + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByKHACH_SAN_ID(string sKHACH_SAN_ID,string sMatch)
{
          string sqlSelect= s_Select()+ " WHERE KHACH_SAN_ID"+ sMatch +sKHACH_SAN_ID + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByTEN_KHACH_SAN(string sTEN_KHACH_SAN)
{
          string sqlSelect= s_Select()+ " WHERE TEN_KHACH_SAN  Like N'%"+ sTEN_KHACH_SAN + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByDIA_CHI(string sDIA_CHI)
{
          string sqlSelect= s_Select()+ " WHERE DIA_CHI  Like N'%"+ sDIA_CHI + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchBySO_DIEN_THOAI(string sSO_DIEN_THOAI)
{
          string sqlSelect= s_Select()+ " WHERE SO_DIEN_THOAI  Like N'%"+ sSO_DIEN_THOAI + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByTINH_THANH(string sTINH_THANH)
{
          string sqlSelect= s_Select()+ " WHERE TINH_THANH  ="+ sTINH_THANH + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByTINH_THANH(string sTINH_THANH,string sMatch)
{
          string sqlSelect= s_Select()+ " WHERE TINH_THANH"+ sMatch +sTINH_THANH + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable Search( string sKHACH_SAN_ID
            , string sTEN_KHACH_SAN
            , string sDIA_CHI
            , string sSO_DIEN_THOAI
            , string sTINH_THANH
            )
 {
       string sqlselect=s_Select() + " WHERE" ;
      if (sKHACH_SAN_ID!=null && sKHACH_SAN_ID!="") 
            sqlselect +=" AND KHACH_SAN_ID =" + sKHACH_SAN_ID ;
      if (sTEN_KHACH_SAN!=null && sTEN_KHACH_SAN!="") 
            sqlselect +=" AND TEN_KHACH_SAN LIKE N'%" + sTEN_KHACH_SAN +"%'" ;
      if (sDIA_CHI!=null && sDIA_CHI!="") 
            sqlselect +=" AND DIA_CHI LIKE N'%" + sDIA_CHI +"%'" ;
      if (sSO_DIEN_THOAI!=null && sSO_DIEN_THOAI!="") 
            sqlselect +=" AND SO_DIEN_THOAI LIKE N'%" + sSO_DIEN_THOAI +"%'" ;
      if (sTINH_THANH!=null && sTINH_THANH!="") 
            sqlselect +=" AND TINH_THANH =" + sTINH_THANH ;
   sqlselect=sqlselect.Replace("WHERE AND","WHERE");
   int n=sqlselect.IndexOf("WHERE");
   if(n==sqlselect.Length -5) sqlselect=sqlselect.Remove(n,5) ;
   return GetTable(sqlselect,sTableName);
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static KHACH_SAN Insert_Object(
string  sTEN_KHACH_SAN
            ,string  sDIA_CHI
            ,string  sSO_DIEN_THOAI
            ,string  sTINH_THANH
            ) 
 { 
              string tem_sTEN_KHACH_SAN=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sTEN_KHACH_SAN,"nvarchar");
              string tem_sDIA_CHI=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sDIA_CHI,"nvarchar");
              string tem_sSO_DIEN_THOAI=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sSO_DIEN_THOAI,"varchar");
              string tem_sTINH_THANH=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sTINH_THANH,"int");

             string sqlSave=" INSERT INTO KHACH_SAN("+
                   "TEN_KHACH_SAN," 
        +                   "DIA_CHI," 
        +                   "SO_DIEN_THOAI," 
        +                   "TINH_THANH) VALUES("
 +tem_sTEN_KHACH_SAN+","
 +tem_sDIA_CHI+","
 +tem_sSO_DIEN_THOAI+","
 +tem_sTINH_THANH +")";
             bool OK = Exec(sqlSave)>=1?true:false;
           if (OK) 
           { 
          KHACH_SAN newKHACH_SAN= new KHACH_SAN();
                 newKHACH_SAN.KHACH_SAN_ID=GetTable( " SELECT TOP 1 KHACH_SAN_ID FROM KHACH_SAN ORDER BY KHACH_SAN_ID DESC ").Rows[0][0].ToString();
              newKHACH_SAN.TEN_KHACH_SAN=sTEN_KHACH_SAN;
              newKHACH_SAN.DIA_CHI=sDIA_CHI;
              newKHACH_SAN.SO_DIEN_THOAI=sSO_DIEN_THOAI;
              newKHACH_SAN.TINH_THANH=sTINH_THANH;
            return newKHACH_SAN; 
           } 
           else return null ;
}
//───────────────────────────────────────────────────────────────────────────────────────
public bool  Save_Object(string sTEN_KHACH_SAN
                ,string sDIA_CHI
                ,string sSO_DIEN_THOAI
                ,string sTINH_THANH
                ) 
 { 
              string tem_sTEN_KHACH_SAN=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sTEN_KHACH_SAN,"nvarchar");
              string tem_sDIA_CHI=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sDIA_CHI,"nvarchar");
              string tem_sSO_DIEN_THOAI=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sSO_DIEN_THOAI,"varchar");
              string tem_sTINH_THANH=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sTINH_THANH,"int");

 string sqlSave=" UPDATE KHACH_SAN SET "+"TEN_KHACH_SAN ="+tem_sTEN_KHACH_SAN+","
 +"DIA_CHI ="+tem_sDIA_CHI+","
 +"SO_DIEN_THOAI ="+tem_sSO_DIEN_THOAI+","
 +"TINH_THANH ="+tem_sTINH_THANH+" WHERE KHACH_SAN_ID="+MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(this.KHACH_SAN_ID,"int identity");;
              bool OK = Exec(sqlSave)>=1?true:false;
           if (OK) 
           { 
                this.TEN_KHACH_SAN=sTEN_KHACH_SAN;
                this.DIA_CHI=sDIA_CHI;
                this.SO_DIEN_THOAI=sSO_DIEN_THOAI;
                this.TINH_THANH=sTINH_THANH;
           } 
 return OK;  }
//───────────────────────────────────────────────────────────────────────────────────────
 #region Update DataColumn  
 public bool Update_KHACH_SAN_ID(string sKHACH_SAN_ID)
{
    string sqlSave= " UPDATE KHACH_SAN SET KHACH_SAN_ID='"+ sKHACH_SAN_ID+ "' WHERE KHACH_SAN_ID='"+ this.KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.KHACH_SAN_ID=sKHACH_SAN_ID;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_TEN_KHACH_SAN(string sTEN_KHACH_SAN)
{
    string sqlSave= " UPDATE KHACH_SAN SET TEN_KHACH_SAN='N"+ sTEN_KHACH_SAN+ "' WHERE KHACH_SAN_ID='"+ this.KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.TEN_KHACH_SAN=sTEN_KHACH_SAN;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_DIA_CHI(string sDIA_CHI)
{
    string sqlSave= " UPDATE KHACH_SAN SET DIA_CHI='N"+ sDIA_CHI+ "' WHERE KHACH_SAN_ID='"+ this.KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.DIA_CHI=sDIA_CHI;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_SO_DIEN_THOAI(string sSO_DIEN_THOAI)
{
    string sqlSave= " UPDATE KHACH_SAN SET SO_DIEN_THOAI='N"+ sSO_DIEN_THOAI+ "' WHERE KHACH_SAN_ID='"+ this.KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.SO_DIEN_THOAI=sSO_DIEN_THOAI;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_TINH_THANH(string sTINH_THANH)
{
    string sqlSave= " UPDATE KHACH_SAN SET TINH_THANH='"+ sTINH_THANH+ "' WHERE KHACH_SAN_ID='"+ this.KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.TINH_THANH=sTINH_THANH;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 #endregion
 #region Update DataColumn  Static 
 public static bool Update_TEN_KHACH_SAN(string sTEN_KHACH_SAN,string s_KHACH_SAN_ID)
{
  string sqlSave= " UPDATE KHACH_SAN SET TEN_KHACH_SAN='N"+sTEN_KHACH_SAN+"' WHERE KHACH_SAN_ID='"+ s_KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_DIA_CHI(string sDIA_CHI,string s_KHACH_SAN_ID)
{
  string sqlSave= " UPDATE KHACH_SAN SET DIA_CHI='N"+sDIA_CHI+"' WHERE KHACH_SAN_ID='"+ s_KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_SO_DIEN_THOAI(string sSO_DIEN_THOAI,string s_KHACH_SAN_ID)
{
  string sqlSave= " UPDATE KHACH_SAN SET SO_DIEN_THOAI='N"+sSO_DIEN_THOAI+"' WHERE KHACH_SAN_ID='"+ s_KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_TINH_THANH(string sTINH_THANH,string s_KHACH_SAN_ID)
{
  string sqlSave= " UPDATE KHACH_SAN SET TINH_THANH='"+sTINH_THANH+"' WHERE KHACH_SAN_ID='"+ s_KHACH_SAN_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
#endregion
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable GetTableAll() 
 {
       return  GetTableAll(null, null);
 }
public static DataTable GetTableAll(string sWhere, params string[] orderFields)
{
   string sqlSelect = " SELECT * FROM KHACH_SAN";
   if (!string.IsNullOrEmpty(sWhere))
      sqlSelect += " where " + sWhere; 
   string order = "";
   if (orderFields != null && orderFields.Length > 0)
     order = string.Join(",", orderFields);
   if (order != "")
      sqlSelect += " ORDER BY " + order;
   return GetTable(sqlSelect,sTableName);
}
//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
public static DataTable GetTableFields(string sWhere, string[] orderFields, params string[] fields)
{
 string field = "";
 if (fields != null && fields.Length > 0)
    field = string.Join(",", fields);
 else field = "*";
 string sqlSelect = string.Format(" SELECT {0} FROM {1} ", field, "KHACH_SAN");
 if (!string.IsNullOrEmpty(sWhere))
    sqlSelect += " where " + sWhere;
 string order = "";
 if (orderFields != null && orderFields.Length > 0)
    order = string.Join(",", orderFields);
 if (order != "")
    sqlSelect += " ORDER BY " + order;
 return GetTable(sqlSelect,sTableName);
 }
 public static DataTable GetTableFields(params string[] fields)
 {
    return GetTableFields(null, null, fields);
 }
 public static DataTable GetTableFields(string[] orderFields, params string[] fields)
 {
    return GetTableFields(null, orderFields, fields);
 }
//───────────────────────────────────────────────────────────────────────────────────────
   private static DataTable dt_KHACH_SAN;
   public static bool Change_dt_KHACH_SAN = true;
   public static bool AllowAutoChange = true;
   public static DataTable get_KHACH_SAN()
   {
   if (dt_KHACH_SAN == null || Change_dt_KHACH_SAN == true)
     {
   dt_KHACH_SAN = GetTableAll();
         Change_dt_KHACH_SAN = true && AllowAutoChange ;
     }
     return dt_KHACH_SAN;
   }
   //───────────────────────────────────────────────────────────────────────────────────────
}  
 } 
